package day25.edu;

import com.sun.source.tree.Tree;
import day24.edu.User;
import org.junit.Test;

import java.util.Comparator;
import java.util.Set;
import java.util.Map;
import java.util.TreeMap;

// Created: 25/09/2025, ChangJiang Ru
public class TreeMapTest {
    @Test
    public void test1()
    {
        TreeMap map = new TreeMap();
        map.put("AA", 90);
        map.put("FF", 80);
        map.put("XX", 40);
        map.put("DD", 70);
        map.put("BB", 20);
        map.put("GG", 100);

        System.out.println(map);

        Set entrySet = map.entrySet();
        for (Object entry : entrySet)
        {
            System.out.println(entry);
        }
    }
    
    @Test
    public void test2()
    {
        Comparator comparator = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof User && o2 instanceof User)
                {
                    User u1 = (User)o1;
                    User u2 = (User)o2;

                    int value = u1.getName().compareTo(u2.getName());
                    if(value != 0)
                    {
                        return value;
                    }

                     return u1.getAge() - u2.getAge();
                }

                throw new RuntimeException("Type not match");
            }
        };

        TreeMap map = new TreeMap(comparator);
        User u1 = new User("Tom", 34);
        User u2 = new User("Jonathan Blow", 23);
        User u3 = new User("Jack", 45);
        User u4 = new User("Tsoding", 23);

        map.put(u1, 34);
        map.put(u2, 23);
        map.put(u3, 78);
        map.put(u4, 56);

        Set entrySet = map.entrySet();
        for (Object entry : entrySet)
        {
            System.out.println(entry);
        }

    }
}
